package com.hmjk.health.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;


public class CakyView extends View{
	private int YELLOW = 0xffffb11a;
	private int GREEN = 0xff04ac10;
	private int RED = 0xffff0000;
	
	private int totalN = 0, normalN = 0, belowN =0, higherN = 0;	
	private int cakyHeight = 0;
	
	public CakyView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public CakyView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}	

	public CakyView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

    public void setHeight(int h)
    {
    	cakyHeight = h;
    }
    
    public void setParam(int total, int normal, int below, int higher)
    {
    	totalN = total;
    	normalN = normal;
    	belowN = below;
    	higherN = higher;
    }
    
    public int getTotalN()
    {
    	return totalN;
    }
    
    public int getNoramlN()
    {
    	return normalN;
    }
    
    public int getBelowN()
    {
    	return belowN;
    }
    
    public int getHigherN()
    {
    	return higherN;
    }
    
   
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
		
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);	
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);		

		heightSize = (int) cakyHeight;

		int hSpec = MeasureSpec.makeMeasureSpec(heightSize,heightMode);		
		super.onMeasure(widthMeasureSpec, hSpec);
	}	
	
	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		super.onLayout(changed, l, t, r, b);
	}
	
	private final static float gap = 4;
	
	@Override
	protected void onDraw(Canvas canvas){
		super.onDraw(canvas);
		
		int cy = this.getHeight()/2;
		int cx = this.getWidth()/2;
		int r = cakyHeight/2;
		int rInner = r/2;
		int thick = r - rInner;
		
		r -= thick/2;
		
		Paint mPaint = new Paint();
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setColor(GREEN);
		//mPaint.setStrokeJoin(Paint.Join.ROUND);   
		//mPaint.setStrokeCap(Paint.Cap.ROUND);   
		mPaint.setStrokeWidth(thick);   		
		mPaint.setAntiAlias(true);  
		
		RectF r1 = new RectF();
		
    	r1.left = cx - r;
    	r1.top = cy - r;
    	r1.right = cx + r;
    	r1.bottom = cy + r;	
    	
    	if(totalN == 0)
    	{
    		canvas.drawArc(r1, 0, 360, false, mPaint);	
    		return;
    	}
    	
    	float green,yellow,red;
    	
    	green = (float)normalN/(float)totalN * 360;
    	yellow = (float)belowN/(float)totalN * 360;
    	red = (float)higherN/(float)totalN * 360;    
    	
    	float x,y,x2,y2;
	
    	//canvas.drawArc(r1, start, end, false, mPaint);
    	float start;
    	start = 270 - green/2;
    	canvas.drawArc(r1, start, green, false, mPaint);	
   	
    	mPaint.setColor(RED);   	
        start = start + green ;  
        start = start%360;
    	canvas.drawArc(r1, start, red, false, mPaint);
    
    
    	mPaint.setColor(YELLOW); 
    	start = start + red;  
    	start = start%360;
    	canvas.drawArc(r1, start, yellow, false, mPaint);
    	
    	
    	/*draw white line*/
    	start = 270 - green/2;
    	mPaint.setColor(Color.WHITE);
    	mPaint.setStrokeWidth(3);
    	
    	r+= thick/2;
		x = (float) ((r) * Math.cos(Math.toRadians(start)))+cx;
		y = (float) ((r) * Math.sin(Math.toRadians(start)))+cy;   
		
		x2 = (float) ((rInner) * Math.cos(Math.toRadians(start)))+cx;
		y2 = (float) ((rInner) * Math.sin(Math.toRadians(start)))+cy; 
        if(green != 0)
    	canvas.drawLine(x, y, x2, y2, mPaint);
    	
    	start += green;
		x = (float) ((r) * Math.cos(Math.toRadians(start)))+cx;
		y = (float) ((r) * Math.sin(Math.toRadians(start)))+cy;   		
		x2 = (float) ((rInner) * Math.cos(Math.toRadians(start)))+cx;
		y2 = (float) ((rInner) * Math.sin(Math.toRadians(start)))+cy; 
		if(red != 0)
    	canvas.drawLine(x, y, x2, y2, mPaint);  
		
    	start += red;
		x = (float) ((r) * Math.cos(Math.toRadians(start)))+cx;
		y = (float) ((r) * Math.sin(Math.toRadians(start)))+cy;   		
		x2 = (float) ((rInner) * Math.cos(Math.toRadians(start)))+cx;
		y2 = (float) ((rInner) * Math.sin(Math.toRadians(start)))+cy; 
		
		if(yellow != 0)
    	canvas.drawLine(x, y, x2, y2, mPaint);   	   	
	}	
	
}